package processjson

import (
	"encoding/json"
	// "fmt"
	io "io/ioutil"
	"sync"

	"ddns/parameter"
)

var file_locker sync.Mutex //config file locker

func LoadConfig(filename string) (parameter.DDNSCfg, error) {
	var conf parameter.DDNSCfg
	file_locker.Lock()
	data, err := io.ReadFile(filename) //read config file
	file_locker.Unlock()
	if err != nil {
		// fmt.Println("read json file error")
		return conf, err
	}
	err = json.Unmarshal(data, &conf)
	if err != nil {
		// fmt.Println("unmarshal json file error")
		return conf, err
	}
	return conf, nil
}

// func main() {
// 	conf, err := LoadConfig("../config/ddns.json")
// 	if err != nil {
// 		fmt.Printf("load config failed: %v\n", err)
// 		return
// 	}
// 	fmt.Println(conf)
// 	fmt.Printf("zones: %v\n", conf.TaskInterval)
// 	fmt.Printf("idx_0_info: %v\n", conf.DomainCfg.Domains[0])
// 	fmt.Printf("idx_0_topdmain: %v\n", conf.DomainCfg.AnalyzeManufactureerId)
// 	// fmt.Printf("getips: %v\n", conf.Zones)
// }
